/**
 * 
 */
package de.oszimtcc.timetablescreens;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.ads.AdActivity;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

import de.oszimtcc.framework.BaseActivity;
import de.oszimtcc.framework.BaseScreen;
import de.oszimtcc.framework.Logger;
import de.oszimtcc.timetable.R;
import de.oszimtcc.timetable.TimetableApplication;
import de.oszimtcc.timetabledialogs.ChooseClassDialog;
import de.oszimtcc.timetableservices.TimetableChangingService;

/**
 * @author reuter
 *
 */
public class ConfigurationScreen extends BaseScreen
{
	Button saveConfiguration;
	Button chooseClass;
	
	public ConfigurationScreen(BaseActivity pBaseActivity)
	{
		super(pBaseActivity);
		Setup(R.layout.configuration);
		initConfiguration();
    }
	
	public ConfigurationScreen(BaseActivity pBaseActivity, Class pLastScreen)
	{
		super(pBaseActivity);
		Setup(R.layout.configuration);
		SetLastScreen(pLastScreen);
		initConfiguration();
    }
	
	private void initConfiguration()
	{
		Logger.Debug("Open ConfigurationScreen");
        
        
        saveConfiguration = (Button)this.activity.findViewById(R.id.configurationSaveButton);
        saveConfiguration.setOnClickListener(onSaveConfiguration);
        
        chooseClass = (Button) this.activity.findViewById(R.id.configurationChooseClassButton);
        chooseClass.setOnClickListener(onChooseClass);
        
        TextView choosenClass = (TextView)activity.findViewById(R.id.configurationChoosenClass);
        if(activity.GetStringPreference("Class") != "")
        	choosenClass.setText(activity.GetStringPreference("Class").replace("+", " "));
	}
	
	OnClickListener onSaveConfiguration = new OnClickListener() 
	{	
		@Override
		public void onClick(View v)
		{
			activity.SetIntPreference("RefreshTime", ((SeekBar)activity.findViewById(R.id.seekBar1)).getProgress());
			activity.SetStringPreference("Class", ((TextView)activity.findViewById(R.id.configurationChoosenClass)).getText().toString().replace(" ", "+"));
			if(GetLastScreen() == null)
				activity.screen = new TimetableScreen(activity);
			else
				activity.screen = InstantiateScreen(GetLastScreen());
		}
	};
	
	OnClickListener onChooseClass = new OnClickListener() 
	{	
		@Override
		public void onClick(View v)
		{
			ChooseClassDialog choose = new ChooseClassDialog(activity,new Handler()
			{
				@Override
				public void handleMessage(Message msg) 
				{
					if(msg.obj != null && msg.obj instanceof String)
					{
						((TextView)activity.findViewById(R.id.configurationChoosenClass)).setText(msg.obj.toString().replace("+", " "));
					}
					else
					{
					}
				}
			}); 
			choose.show();
		}
	};
}
